xlv.obj.inputMaskDate={
	extend:function(){
		this.extend('xlv.obj.inputMask');
	},
	implement:function(){
	},
	create:function(tag){
		var div=document.createElement((tag||'div'));
		div.setAttribute('xlvobject','xlv.obj.inputMaskDate');
		return div;			
	},
	properties:{
		datePattern:'yyyy/MM/dd'
	},
	method:{
		initial:function(){
			this._pattern2Mask();
			this.$super('xlv.obj.inputMask.initial');
			this.addEventHandler('dblclick', '_dblclick');
		},
		_dblclick:function(event){
			var dim=Element.getDimensions(this.editor_getEditor());
			var lt=this.getDesktop().viewportOffset(this.editor_getEditor());
			this.$V.calendar=xlv.sys.newInstance(this.getDesktop(),'calendar');
			this.$V.calendar.setDate(new Date().parseDate(this.valueWithMask(),this.$P.datePattern));
			var style={position:'absolute',zIndex:xlv.sys.zIndexPopup,display:''};
			var caldim=Element.getDimensions(this.$V.calendar);
			style.left=(lt.left+(dim.width-caldim.width)/2)+'px';
			style.top=(lt.top-12+dim.height)+'px';
			Element.setStyle(this.$V.calendar,style);
			this.addEventHandler('xlv:select_changed','_calendar_select_changed',this.$V.calendar);
		},
		_calendar_select_changed:function(event){
			event.stop();
			if(event.memo)this.editor_valueToEditor(event.memo.to.formatDate());
			this.removeObserve('xlv:select_changed','_calendar_select_changed',this.$V.calendar);
			this.$V.calendar.parentNode.removeChild(this.$V.calendar);
			this.$V.calendar=null;
			this.editor_focus();
		},
		_editor_document_mousedown:function(event){
			if(this.$V.calendar){
				if(!xlv.util.isDescendantOf(event.target,this.$V.calendar)){
					event.memo=null;
					this._calendar_select_changed(event);
				}
			}else{
				if(!xlv.util.isDescendantOf(event.target,this)){
					event.stop();
					this._editor_endEdit(true);
				}
			}
		},
		_blur:function(event){
			if(this.editor_editing()&&!this.$V.calendar)this._editor_endEdit(true);
		},
		_pattern2Mask:function(){
			var p=this.$P.datePattern;
			this.$P.mask=p.replace(/[y|c|M|d|h|m|s]/g,'9');
		},
		isValidInput:function(p,keycode) {
			var valid=this.$super('xlv.obj.inputMask.isValidInput',p,keycode);
			if(valid){
				var key=keycode-48;
				var value=this.$V.editor.value;
				var pt=this.$P.datePattern;
				var cc=pt.charAt(p);
				var is1=(pt.charAt(p+1)==cc);
				switch(cc){
				case 'M':
					if(is1){
						if(key>1) return false;
					}else{
						var v1=(''+value.charAt(p-1))*10+key;
						if(v1>12||v1<=0)return false;
					}
					break;
				case 'd':
					if(is1){
						var MM=value.substr(pt.indexOf('MM'),2);
						if(MM==2&&key>=3)return false;
						if(key>3) return false;
					}else{
						var v1=(''+value.charAt(p-1))*10+key;
						if(v1>31)return false;
						var yy=value.substr(pt.indexOf('yyyy'),4);
						if(!yy)	yy=1911+1*value.substr(pt.indexOf('cyy'),3);
						var MM=value.substr(pt.indexOf('MM'),2);
						var chk=new Date(yy,MM-1,v1);
						if(chk.getDate()!=v1) return false;
					}
					break;
				case 'H':
					if(is1){
						if(key>2)return false;
					}else{
						var v1=(''+value.charAt(p-1))*10+key;
						if(v1>=24)return false;
					}
					break;
				case 'm':		
				case 's':
					if(is1){
						if(key>=6)return false;
					}else{
						var v1=(''+value.charAt(p-1))*10+key;
						if(v1>=60)return false;
					}
					break;
				}
			}
			return valid;
		},
		editor_valueToEditor:function(value){
			this.$V.editor.value=new Date().parseDate(value).formatDate(this.$P.datePattern);
			this._validateMask();
		},
		editor_valueFromEditor:function(){
			return new Date().parseDate(this.valueWithMask(),this.$P.datePattern).formatDate();
		},
		editor_validate:function(){
			var chk=new Date().parseDate(this.valueWithMask(),this.$P.datePattern);
			var valid=chk.validate;
			if(valid) Element.removeClassName(this.$V.editor,'validate_error');
			else Element.addClassName(this.$V.editor,'validate_error');
			return valid;
		}
	}
};
